<?php
namespace App\Http\Car\v1\Controllers;

use App\Exceptions\WorkException;

use App\Models\User;
use App\Notifications\VerifcationCode;

use App\Services\UserServices;
use Illuminate\Http\Request;
use  App\Util\Code;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Notification;
use Leonis\Notifications\EasySms\Channels\EasySmsChannel;
use Overtrue\EasySms\PhoneNumber;
use App\Http\Car\v1\Controllers\CarController;
use Illuminate\Http\JsonResponse;

use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use App\Services\WxServices;

use Illuminate\Support\Facades\Http;


class AuthController extends CarController{

    protected $only = 'user';

    public function user()
    {
        $user = Auth::guard('car')->user();

        return $this ->succ($user);
    }

    public function login(  Request $request ){
        //哈希加密
        $str = '12345678';

        return password_hash($str,PASSWORD_DEFAULT);

        //1 第一步获取小程序传递信息验证参数

        $code       = $this -> checkString("code" );
        $nickName   = $this -> checkTitle("nick_name");
        $mobile     = $this -> checkMobile('mobile');



        $openId = WxServices::getInstance( )->getByOpenId( $code );


        //2 查询此用户是否存在如果存在直接登录
        $user = UserServices::getInstance()->getByOpenId( $openId );
        if ( !is_null($user) ){

            $token = Auth::guard('car')->login($user);
            return $this->succ(['token' => $token,'user_info'=>$user]);

        }


        //3如果此用户不存在创建用户并走登录逻辑

        $user = new User();
        $user -> nick_name  = $nickName;
        $user -> mobile     = $mobile;
        $user -> open_id    = $openId;
        $user -> password   = '123456';
        //$user->avatar     = "https://yanxuan.nosdn.127.net/80841d741d7fa3073e0ae27bf487339f.jpg?imageView&quality=90&thumbnail=64x64";
        $user -> save();


        $token = Auth::guard('car')->login($user);
        return $this->succ(['token' => $token,'user_info'=>$user]);

    }





    public function register( Request $request )
    {

////       $data = Http::get('https://api.weixin.qq.com/sns/jscode2session', [
////            'appid'      => 'wxcd609e020db99085',
////            'secret'     => 'c3763c7f236952bf3894a328bba0ff55',
////            'js_code'    => '081I9o0006sQ2N1LDw100HqBNj1I9o0d',
////            'grant_type' => 'authorization_code',
////        ]);
//
//        $data = Http::get('cgi-bin/token', [
//            'grant_type'      => 'client_credential',
//            'appid'     => 'wxcd609e020db99085',
//            'secret'    => 'c3763c7f236952bf3894a328bba0ff55',
//
//        ]);
//        $data = json_decode( $data);
//
//
//
//        $response = Http::post('https://api.weixin.qq.com/wxa/business/getuserphonenumber', [
//            'access_token' =>   $data->access_token,
//            'code' => '061L2Y000MVq3N1RlA3000tlN43L2Y0r',
//        ]);
//
//        $data = json_decode( $response);
//
//        dd( $data);


//        $response = Http::post('https://api.weixin.qq.com/wxa/business/getuserphonenumber', [
//            'name' => 'Steve',
//            'role' => 'Network Administrator',
//        ]);


        #1验证传递参数是否合法

        //$this->checkInteger('nick_name');
        //$nickName = $request->input('nick_name');

//        $password = $request->input('password');
//        $mobile   = $request->input('mobile');
//        $code     = $request->input('code' );
//        $wxCode   = $request->input('wx_code' );

//        if ( empty( $mobile ) || empty( $code ) || empty( $wxCode ) ) {
//
//            return $this ->fail( Code::CLT_ILLEGAL );
//        }

        throw new WorkException(Code::CLT_ILLEGAL);
        #2 根据前端传递code码获取openId



        #3 查询此用户是否已经存在如果不存在创建一条新纪录

        //根据openid查询数据库记录
        $user = UserServices::getInstance()->getByOpenId( "ceeeeeee" );
       // dd( $user );
//        if (!is_null($user)) {
//            return $this->fail(Code::NAME_REGISTERED);
//        }


        #4响应客户端信息返回token

        $token=Auth::guard('car')->login($user);

        return $this->succ(['token'=>$token]);

    //        Notification::route(EasySmsChannel::class, new PhoneNumber($mobile,86))->notify(new VerifcationCode($code));
    //
    //        return ["err"=>0,'msg'=>'成功'];
    }
}